package cn.harvest.storage.bo.manager;

import java.util.List;

import cn.harvest.storage.dao.DAO;
import cn.harvest.storage.po.Merchandise;

public class MerchandiseService {

	private DAO dao = DAO.getDAO();

	private MerchandiseService() {

	}

	public static MerchandiseService getService() {

		MerchandiseService service = new MerchandiseService();
		return service;
	}

	public boolean create(Merchandise merchandise) {

		return dao.create(merchandise);
	}
	
	public boolean update(Merchandise merchandise) {
		
		return dao.update(merchandise);
	}

	public List selectBySQL(Merchandise merchandise,String parentType) {

		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT m.merchandise_id, m.merchandise_cid, m.merchandise_name, mt.merchandise_type_name, m.merchandise_norm,");
		sql.append("		m.merchandise_amount, mu.merchandise_unit_name, m.merchandise_low_alert, m.merchandise_memo, mt.merchandise_type_parent,");
		sql.append("		m.merchandise_type_id, m.merchandise_unit_id, m.merchandise_height_alert");
		sql.append(" FROM merchandise m, merchandise_type mt, merchandise_unit mu");
		sql.append(" WHERE 1=1");
		sql.append(" AND m.merchandise_unit_id = mu.merchandise_unit_id");
		sql.append(" AND m.merchandise_type_id = mt.merchandise_type_id");
		if (null != merchandise.getMerchandiseId()) {
			sql.append(" AND m.merchandise_id = " + merchandise.getMerchandiseId());
		}
		if (null != merchandise.getMerchandiseName()) {
			sql.append(" AND m.merchandise_name LIKE '%" + merchandise.getMerchandiseName() + "%'");
		}
		if (null != merchandise.getMerchandiseType()) {
			sql.append(" AND m.merchandise_type_id = " + merchandise.getMerchandiseType().getMerchandiseTypeId());
		}
		if (null != parentType && !"".equals(parentType)) {
			sql.append(" AND mt.merchandise_type_parent = " + parentType);
		}
		sql.append(" ORDER BY m.merchandise_id;");

		System.out.println("### - MerchandiseService -> selectBySQL -> #SQL \r\n	 " + sql);
		
		return dao.selectBySQL(sql.toString());
	}
	
	public Merchandise selectByHQL(String merchandiseId) {
		StringBuffer hql = new StringBuffer();
		hql.append(" FROM Merchandise m WHERE m.merchandiseId = " + merchandiseId);
		
		System.out.println("### - MerchandiseService -> selectByHQL -> #HQL \r\n	 " + hql);
		
		Merchandise merchandise = (Merchandise) dao.selectByHQL(hql.toString()).get(0);
		return merchandise;
	}
	
	public static void main(String[] args) {

	}
}
